package com.ruiyin.utils;

import org.apache.commons.io.FileUtils;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import java.io.File;
import java.io.IOException;

public class SpiderUtils {
    //配置请求超时链接超时信息
    public RequestConfig getConfig()
    {
        System.setProperty("https.protocols", "TLSv1.2");
        RequestConfig  config = RequestConfig.custom().setConnectTimeout(6000).setConnectionRequestTimeout(6000).setSocketTimeout(6000).build();
        return config;
    }

    private static int MAX_TOTAL = 50;
    private static int MAX_PER_ROUTE = 24;
    private PoolingHttpClientConnectionManager cm;
    public SpiderUtils(){
        this.cm = new PoolingHttpClientConnectionManager();
    }

    //下载图片
    public void downImg(String url,String dir) {
        String path = "C:\\images\\";
        this.cm.setMaxTotal(MAX_TOTAL);
        this.cm.setDefaultMaxPerRoute(MAX_PER_ROUTE);

        HttpGet httpGet = new HttpGet(url);

        httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.6)");
        httpGet.addHeader("cookie","Hm_lvt_dbc355aef238b6c32b43eacbbf161c3c=1575464236; Hm_lpvt_dbc355aef238b6c32b43eacbbf161c3c=1575465527");
        httpGet.addHeader("accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3");
        httpGet.addHeader("path","https://www.mzitu.com/");
        httpGet.addHeader("referer","https://www.mzitu.com/xinggan/");
        httpGet.addHeader("authority","www.mzitu.com");

        CloseableHttpClient client = HttpClients.custom().setConnectionManager(this.cm).setRetryHandler(new DefaultHttpRequestRetryHandler(5, true)).build();
        CloseableHttpResponse response = null;
        httpGet.setConfig(getConfig());

        try {
            response = client.execute(httpGet);
            if (response.getStatusLine().getStatusCode() == 200) {
                if (response.getEntity() != null) {
                    String[] imgName = url.toString().split("/");
                    File file = new File(path + dir.replaceAll("\\?","").replaceAll("'","").replaceAll(":","") + "\\" +imgName[5]);
                    FileUtils.copyToFile(response.getEntity().getContent(),file);
                    System.err.println("信息:" + file);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }
}
